//
//  AfanMainViewontroller.swift
//  weibo
//
//  Created by afan on 16/3/25.
//  Copyright © 2016年 阿范. All rights reserved.
//

import UIKit

class AfanMainViewontroller: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        addchildViewControllers()
        
        
        ///用 KVC 给 tarBar 重新赋值
        setValue(AfanMainTaBar(), forKey: "tabBar")

    }
    
    
    func addchildViewControllers() {
        // 设置tabBar控制器的子控制器
        // 首页
        let homeVC = AfanHomeController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        // 消息
        let messageVC = AfanMessageController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        // 发现
        let discoverVC = AfanDiscoverController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        // 我
        let profileVC = AfanProfileController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(controller: UIViewController,title: String,imageName: String){
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        let higthImageName = imageName + "_highlighted"
        
        
        ///选中状态会被系统渲染成蓝色. text 同样
        controller.tabBarItem.selectedImage = UIImage(named: higthImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
        ///用 NavigationController 包装起来
        addChildViewController(UINavigationController(rootViewController: controller))
        
    }

}
